﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using IHISCETeim.BLL;
using IHISCETeim.Library;
using IHISCETeim.Model;
using System.Data;
using System.Text;
using System.Reflection;
namespace IHISCETeim.Web.MachineSys.MacModel
{
    public partial class OQualityStrategyDuiCeEdit : PageBase
    {
        protected override Permission[] PowerKey
        {
            get
            {
                return new Permission[] { Permission.修改 };
            }
        }
        protected override string DWZRef
        {
            get
            {
                return "OQualityStrategyListEasyUI";
            }

        }

        protected int QSID { get { return GetRequestValue<int>("QSID",0); } }

        protected string QSType { get { return GetRequestValue<string>("type", ""); } }

        private string _QSTag = null;
        protected string QSTag {
            get {
                if (_QSTag == null)
                    _QSTag= QSType == "l" ? "LDC_" : "YDC_";
                return _QSTag;
            }
        }

        private OQualityStrategyInfo _OQualityStrategyObj = null;
        protected OQualityStrategyInfo OQualityStrategyObj
        {
            get
            {
                if (_OQualityStrategyObj == null)
                    _OQualityStrategyObj = OQualityStrategyController.GetByID(QSID);
                return _OQualityStrategyObj == null ? new OQualityStrategyInfo() { } : _OQualityStrategyObj;
            }
        }

        private List<OQualityStrategyDetailInfo> _listDetail = null;
        /// <summary>
        /// 所有管制明细
        /// </summary>
        protected List<OQualityStrategyDetailInfo> ListDetail
        {
            get
            {
                if (_listDetail == null)
                    _listDetail = OQualityStrategyController.GetListDetailByQSID(QSID);
                return _listDetail == null ? new List<OQualityStrategyDetailInfo>() { } : _listDetail;
            }
        }

        private List<OQualityStrategyAttachmentInfo> _listAttDetail = null;
        /// <summary>
        /// 所有附件明细
        /// </summary>
        protected List<OQualityStrategyAttachmentInfo> ListAttDetail
        {
            get
            {
                if (_listAttDetail == null)
                    _listAttDetail = OQualityStrategyController.GetListAttachmentByQSID(QSID);
                return _listAttDetail == null ? new List<OQualityStrategyAttachmentInfo>() { } : _listAttDetail;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (GetRequestValue<string>("ajaxmethod", "") == "update")
            {
                addmenu();
            }
           // OQualityStrategyInfo oo = new OQualityStrategyInfo();
            binddate();
        }

        protected void binddate()
        { 
            string AttType=QSTag.ToLower()+"scheme";
            List<OQualityStrategyAttachmentInfo> _attschemelist = ListAttDetail.Where(e => e.AttType == AttType).ToList();
            if (_attschemelist.Count<=0)
            {
                _attschemelist.Add(new OQualityStrategyAttachmentInfo() { ID=0, AttType = AttType, QSID = QSID });
                _attschemelist.Add(new OQualityStrategyAttachmentInfo() { ID =0, AttType = AttType, QSID = QSID });
                _attschemelist.Add(new OQualityStrategyAttachmentInfo() { ID =0, AttType = AttType, QSID = QSID });
            }
            this.rpSchemeAtt.DataSource = _attschemelist;
            this.rpSchemeAtt.DataBind();
            // rpSendsvrBulletin
            AttType = QSTag.ToLower() + "sendsvrbulletin";
            _attschemelist = ListAttDetail.Where(e => e.AttType == AttType).ToList();
            if (_attschemelist.Count <= 0)
            {
                _attschemelist.Add(new OQualityStrategyAttachmentInfo() { ID = 0, AttType = AttType, QSID = QSID });              
            }
            this.rpSendsvrBulletin.DataSource = _attschemelist;
            this.rpSendsvrBulletin.DataBind();

            //
            string QSClass = "p";
            List<OQualityStrategyDetailInfo> listdetail = ListDetail.Where(e => e.QSType == QSType && e.QSClass == QSClass).ToList();
            //if (listdetail.Count <= 0)
            //{
            //    for (int index = 0; index < 10; index++)
            //    {
            //        listdetail.Add(new OQualityStrategyDetailInfo() { QSClass = QSClass, QSType = QSType, QSID = QSID });
            //    }
            //}

            this.rpPartList.DataSource = listdetail;
            this.rpPartList.DataBind();
            ///rpSerList
            QSClass = "m";
            listdetail = ListDetail.Where(e => e.QSType == QSType && e.QSClass == QSClass).ToList();
            //if (listdetail.Count <= 0)
            //{
            //    for (int index = 0; index < 10; index++)
            //    {
            //        listdetail.Add(new OQualityStrategyDetailInfo() { QSClass = QSClass, QSType = QSType, QSID = QSID});
            //    }
            //}

            this.rpSerList.DataSource = listdetail;
            this.rpSerList.DataBind();
        }

        protected string GetClassPropertyValue(string propertyName)
        {
          
            propertyName = QSTag + propertyName;
            return getProperties<OQualityStrategyInfo>(OQualityStrategyObj, propertyName);
        }

        /// <summary>
        ///   /遍历获取类的属性及属性的值：
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="t"></param>
        /// <returns></returns>
        public string getProperties<T>(T t,string propertyName)
        {
　　        string tStr = string.Empty;
　　        if (t == null)
　　        {
　　　　        return tStr;
　　        }
　　        System.Reflection.PropertyInfo[] properties = t.GetType().GetProperties(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public);
    
　　        if (properties.Length <= 0)
　　        {
　　　　        return tStr;
　　        }
　　        foreach (System.Reflection.PropertyInfo item in properties)
　　        {
　　　　        string name = item.Name;
　　　　        object value = item.GetValue(t, null);
                if (propertyName.ToLower().Trim() == name.ToLower().Trim())
                {
                    return value.ToString();
                    break;
                }
            //if (item.PropertyType.IsValueType || item.PropertyType.Name.StartsWith("String"))
            //{
            //  tStr += string.Format("{0}:{1},", name, value);
            //}
            //else
            //{
            //        getProperties(value, propertyName);
            //}
　　        }
　　        return tStr;
        }

        protected void addmenu()
        {
            char[] caSplit = new char[] { ',' };
            string[] IDs = GetRequestValue<string>("ID", "").Split(caSplit);
            string[] AttTypes = GetRequestValue<string>("AttType", "").Split(caSplit);
            string[] Remarks = GetRequestValue<string>("Remark", "").Split(caSplit);
            string[] AttPaths = GetRequestValue<string>("AttPath", "").Split(caSplit);

            string[] QSTypes = GetRequestValue<string>("QSType", "").Split(caSplit);
            string[] QSClasss = GetRequestValue<string>("QSClass", "").Split(caSplit);
            string[] PartSerNums = GetRequestValue<string>("PartSerNum", "").Split(caSplit);
            string[] RelevantSteel1s = GetRequestValue<string>("RelevantSteel1", "").Split(caSplit);
            string[] RelevantSteel2s = GetRequestValue<string>("RelevantSteel2", "").Split(caSplit);
            string[] RelevantSteel3s = GetRequestValue<string>("RelevantSteel3", "").Split(caSplit);
            string[] StorageLocations = GetRequestValue<string>("StorageLocation", "").Split(caSplit);
            string[] IsProgresss = GetRequestValue<string>("IsProgress", "").Split(caSplit);
            string[] ProgressDates = GetRequestValue<string>("ProgressDate", "").Split(caSplit);
            string[] detailRemarks = GetRequestValue<string>("detailRemark", "").Split(caSplit);

            


            PropertyInfo[] propertys = OQualityStrategyObj.GetType().GetProperties();
            foreach (PropertyInfo property in propertys)
            {
                if (property.Name.IndexOf(QSTag)>0)
                    property.SetValue(OQualityStrategyObj, GetRequestValue<string>(property.Name, ""), null);                
            }

            if (QSType == "l")
            {
                OQualityStrategyObj.LDC_Scheme = GetRequestValue<string>("LDC_Scheme", "");
                OQualityStrategyObj.LDC_IsExport = GetRequestValue<string>("LDC_IsExport", "");
                OQualityStrategyObj.LDC_ControlDate = GetRequestValue<string>("LDC_ControlDate", "");
                OQualityStrategyObj.LDC_ControlSteel = GetRequestValue<string>("LDC_ControlSteel", "");
                OQualityStrategyObj.LDC_ControlOther = GetRequestValue<string>("LDC_ControlOther", "");
                OQualityStrategyObj.LDC_SendSvrBulletin = GetRequestValue<string>("LDC_SendSvrBulletin", "");
                OQualityStrategyObj.LDC_HandleItemCode = GetRequestValue<string>("LDC_HandleItemCode", "");
                OQualityStrategyObj.LDC_HandleItemName = GetRequestValue<string>("LDC_HandleItemName", "");
            }
            else {
                OQualityStrategyObj.YDC_Scheme = GetRequestValue<string>("YDC_Scheme", "");
                OQualityStrategyObj.YDC_IsExport = GetRequestValue<string>("YDC_IsExport", "");
                OQualityStrategyObj.YDC_ControlDate = GetRequestValue<string>("YDC_ControlDate", "");
                OQualityStrategyObj.YDC_ControlSteel = GetRequestValue<string>("YDC_ControlSteel", "");
                OQualityStrategyObj.YDC_ControlOther = GetRequestValue<string>("YDC_ControlOther", "");
                OQualityStrategyObj.YDC_SendSvrBulletin = GetRequestValue<string>("YDC_SendSvrBulletin", "");
                OQualityStrategyObj.YDC_HandleItemCode = GetRequestValue<string>("YDC_HandleItemCode", "");
                OQualityStrategyObj.YDC_HandleItemName = GetRequestValue<string>("YDC_HandleItemName", "");
            }


            List<OQualityStrategyDetailInfo> listDetail = new List<OQualityStrategyDetailInfo>();
            for (int index = 0; index < QSTypes.Count(); index++)
            {
                if (string.IsNullOrEmpty(QSTypes[index]))
                    continue;

                OQualityStrategyDetailInfo itm = new OQualityStrategyDetailInfo();
                itm.QSType = QSTypes[index];
                itm.QSClass = QSClasss[index];
                itm.PartSerNum = PartSerNums[index];
                itm.RelevantSteel1 = RelevantSteel1s[index];
                itm.RelevantSteel2 = RelevantSteel2s[index];
                itm.RelevantSteel3 = RelevantSteel3s[index];
                itm.StorageLocation = StorageLocations[index];
                itm.IsProgress = IsProgresss[index];
                itm.ProgressDate = ProgressDates[index];
                itm.Remark = detailRemarks[index];

                listDetail.Add(itm);
            }

            List<OQualityStrategyAttachmentInfo> listAtt = new List<OQualityStrategyAttachmentInfo>();
            for (int index = 0; index < IDs.Count(); index++)
            {
                if (string.IsNullOrEmpty(IDs[index]))
                    continue;

                OQualityStrategyAttachmentInfo itm = new OQualityStrategyAttachmentInfo();
                itm.ID =Convert.ToInt32( IDs[index]);
                itm.AttType = AttTypes[index];
                itm.AttPath = AttPaths[index];
                itm.Remark = Remarks[index];

                listAtt.Add(itm);
            }

            int r = OQualityStrategyController.UpdateDuiCe(QSType,OQualityStrategyObj, listDetail, listAtt);
            if (r == -1)
            {
                ErrorJson("管理失败");
            }
            if (r > 0)
            {
                SuccessJson("成功保存",this.DWZRef);
            }
        }
    }

    
}